#!/bin/bash
#kubesphere
# https://kubesphere.io/zh/docs/quick-start/minimal-kubesphere-on-k8s/
# https://github.com/kubesphere/ks-installer 3.0.0做的测试

#在上面安装好的k8s上部署kubesphere
#官网方法
:<<EOF
安装前提条件 https://kubesphere.io/zh/docs/installing-on-kubernetes/introduction/prerequisites/
kubectl apply -f https://raw.githubusercontent.com/kubesphere/ks-installer/v3.0.0/deploy/kubesphere-installer.yaml
kubectl apply -f https://raw.githubusercontent.com/kubesphere/ks-installer/v3.0.0/deploy/cluster-configuration.yaml
EOF
# if [ $# -eq 1 ]; then
#     echo "offline install kubesphere..."
#     docker images | grep kubesphere || (
#         if [ -f kubesphere3-deps.tar ]; then
#         #kubesphere.io官网离线3.0.0版本的镜像抽取的部分先导出docker镜像【imgsave.sh】，然后在离线安装机器上导入
#         # docker load < kubesphere3-deps.tar
#         #或者
#         #在线安装好以后导出的 ../install-ks.sh 可以安装
#          echo "loaded docker image"
#         else
#          echo "docker image from internet"
#          exit 1
#         fi
#     )
# fi

# kubectl --kubeconfig=./config  #在没有预先配置的kubeconfig的主机上执行时可以使用这个方式
kubectl delete -f kubesphere-installer.yaml
kubectl delete -f cluster-configuration.yaml
kubectl delete -f sc.yaml
rm -rf /opt/data/*

mkdir -p /opt/data/{redis,etcd,minio,mysql,openldap,prometues}
kubectl apply -f sc.yaml #需要在k8s dashboard中看下pvc的状态是否正常
kubectl apply -f kubesphere-installer.yaml
kubectl apply -f cluster-configuration.yaml

echo "wait for kubesphere deploy..."
#while ! kubectl get svc -A|grep "ks-console" 
while ! kubectl get po -A|grep "ks-apiserver" | grep "Running"
do
    echo "wait kubesphere..."
    sleep 5
done
while ! kubectl get po -A|grep "ks-console" | grep "Running"
do
    echo "wait kubesphere..."
    sleep 5
done
while ! kubectl get po -A|grep "ks-controller-manager" | grep "Running"
do
    echo "wait kubesphere..."
    sleep 5
done

#执行中出现 PVC问题时可以执行如下再修复一次就可以了
#这个券偶尔出现问题，修订一下
kubectl get pvc -A|grep "prometheus-k8s-db-prometheus-k8s-0" |grep "Bound" || (
    kubectl delete -n kubesphere-monitoring-system persistentvolumeclaim prometheus-k8s-db-prometheus-k8s-0
    kubectl apply -f prometheus.yaml
)

kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}')

# 在11机器上
# docker logs $(docker ps|grep k8s_installer_ks-installer| awk '{print $1}') -f

#管理门户
# kubectl get svc/ks-console -n kubesphere-system
:<<EOF
kubesphere的访问门户
Console: http://192.168.99.10:30880
Account: admin
Password: P@88w0rd

request to http://ks-apiserver.kubesphere-system.svc/oauth/token failed, reason: connect EHOSTUNREACH 10.105.74.20:80
[root@kubesphere-dev vagrant]# kubectl get svc -n kubesphere-system
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
ks-apiserver            ClusterIP   10.105.74.20     <none>        80/TCP         21m
ks-console              NodePort    10.109.182.164   <none>        80:30880/TCP   21m
ks-controller-manager   ClusterIP   10.97.63.213     <none>        443/TCP        21m
openldap                ClusterIP   None             <none>        389/TCP        21m
redis                   ClusterIP   10.109.155.205   <none>        6379/TCP       21m

[root@kubesphere-dev vagrant]# curl http://10.105.74.20 
curl: (7) Failed connect to 10.105.74.20:80; 没有到主机的路由

[root@kubesphere-node1 kubesphere3]# curl http://ks-apiserver.kubesphere-system.svc/oauth/token
curl: (7) Failed connect to ks-apiserver.kubesphere-system.svc:80; 拒绝连接
[root@kubesphere-dev vagrant]# curl http://ks-apiserver.kubesphere-system.svc/oauth/token
curl: (7) Failed connect to ks-apiserver.kubesphere-system.svc:80; 拒绝连接
EOF
#查看ipvs模式的信息 ks-apiserver
# kubectl get svc -n kubesphere-system|grep ks-apiserver
# ipvsadm -Ln | grep -A 3 $(kubectl get svc -n kubesphere-system|grep ks-apiserver|awk '{print $3}')

# ipvs存在指向的pod地址错误，是k8s还是kubesphere的bug呢？未探索【分析过程参考diag.md的描述】
echo "服务都正常后，如果存在无法访问的情况，执行 ipvsadm -C 等待一会后即可"